// pages/myodear/myodear.js
import {
    findWxReservationPeople,
    //取消订单
    cancelReservation,
    //删除预约
    deleteReservation
} from "../../api/home"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        activeIndex: 0,
        Tobeused:"待参观",
        Tobeused1:"已使用",
        Tobeused2:"已取消",
        Tobeused3:"已过期",
        tabs: [{
            id: 1,
            title: "全部"
        }, {
            id: 2,
            title: "待参观"
        }, {
            id: 3,
            title: "已使用"
        }, {
            id: 4,
            title: "已取消"
        }, {
            id: 5,
            title: "已过期"
        }],
        waitUse:false,
        useds:false,
        canceled:false,
        past:false,
        used:[]
    },
    //录入人脸进详情页
    quxiao(e){
        console.log(e.currentTarget.dataset.id);
        wx.navigateTo({
          url: '/pages/myidearok/mydearok?id='+ e.currentTarget.dataset.id,
        })
    },
    //定义删除的方法
    async deleteReservation(id){
        let res = await deleteReservation(id)
        this.findWxReservationPeople(0)
        console.log(res);
    },
    //已使用
    delete1(e){
      console.log(e.currentTarget.dataset.id);
      console.log(this.data.used[e.currentTarget.dataset.index]);
      let id = this.data.used[e.currentTarget.dataset.index].id
      let _this = this;
      wx.showModal({
          title: '温馨提示',
          content: '确定要删除吗',
          success (res) {
            if (res.confirm) {
              // console.log('用户点击确定2222')
              _this.deleteReservation({id})
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
    },
    //已取消删除
    delete2(e){
      console.log(e.currentTarget.dataset.id);
      console.log(this.data.used[e.currentTarget.dataset.index]);
      let id = this.data.used[e.currentTarget.dataset.index].id
      let _this = this;
      wx.showModal({
          title: '温馨提示',
          content: '确定要删除吗',
          success (res) {
            if (res.confirm) {
              // console.log('用户点击确定2222')
              _this.deleteReservation({id})
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
    },
    //已过期删除
    delete3(e){
    console.log(e);
        console.log(e.currentTarget.dataset.id);
        console.log(this.data.used[e.currentTarget.dataset.index]);
        let id = this.data.used[e.currentTarget.dataset.index].id
        let _this = this;
        wx.showModal({
            title: '温馨提示',
            content: '确定要删除吗',
            success (res) {
              if (res.confirm) {
                // console.log('用户点击确定2222')
                _this.deleteReservation({id})
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
    },
    //用户点击取消
    examine(e){
      console.log(e.currentTarget.dataset.id);
      let _this = this
      wx.showModal({
        title: '确定要取消这条预约记录吗？',
        success (res) {
          if (res.confirm) {
            console.log('用户点击确定')
            cancelReservation({
              id:e.currentTarget.dataset.id
            }).then(res=>{
              console.log(res);
              if(res.code == 1000){
                _this.findWxReservationPeople(0)
              }else{
                wx.showToast({
                  title: res.data.msg,
                  icon:"error"
                })
              }
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
     
      

    },
    //点击打开预约码
    dakai(e){
        console.log(this.data.used);
        console.log(e.target.dataset.index);
        const index = e.target.dataset.index
        console.log(e);
        console.log(this.data.used[index].bookType);
        wx.navigateTo({
          url: '/pages/myidearok/mydearok?id='+ e.currentTarget.dataset.id + "&bookType=" + this.data.used[index].bookType + "&travelDate="+  this.data.used[index].travelDate + "&sessionTime=" +  this.data.used[index].sessionTime + "&dayType=" + this.data.used[index].dayType,
        })
    },
    //返回上一页
    onClickLeft() {
        wx.navigateBack({
            delta: 1,
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    // 点击哪一个哪一个加上active  活性
    changTabs(e) {
        console.log(this.data.activeIndex); //待使用是哪个的字段
        console.log(this.data.used);
        let index = e.currentTarget.dataset.index
        this.setData({
            activeIndex: index
        })
    },
    onLoad(options) {
       this.findWxReservationPeople(0)
    },
    //请求列表
    async findWxReservationPeople(status){
        wx.showLoading({
            title: '加载中',
          })
          const res = await findWxReservationPeople({
            status
        })
        console.log(res.data);
        // 待使用是否存在
        const waitUse = res.data.some(item => {
            return item.status == 1;
        })
        // 已使用是否存在
        console.log(waitUse);
        const useds = res.data.some(item => {
            return item.status == 2;
        })
        console.log(useds);
        // 已取消
        const canceled = res.data.some(item => {
            return item.status == 3;
        })
        console.log(canceled);
        // 已过期
        const past = res.data.some(item => {
            return item.status == 4;
        })
        console.log(this.data.used);
        this.setData({
            used:res.data,
            past,
            useds,
            waitUse,
            canceled
        })
        console.log(this.data.used);
        wx.hideLoading()
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.findWxReservationPeople(0)
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})